import 'package:app/constants/http_constants.dart';
import 'package:app/http/api_service.dart';
import 'package:app/models/user.dart';
import 'package:app/router/router.dart';
import 'package:app/store/index.dart';
import 'package:app/widgets/modal.dart';
import 'package:creato_ui/creato_ui.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

class UserInfoPage extends StatefulWidget {
  const UserInfoPage({super.key});

  @override
  State<UserInfoPage> createState() => _UserInfoState();
}

class _UserInfoState extends State<UserInfoPage> {
  CreatoListTileTheme theme = CreatoListTileTheme(
      titleTextFontWeight: FontWeight.normal,
      trailingTextColor: const Color(0xFF747474),
      trailingTextFontSize: 15,
      placeholderColor: const Color(0xFF999999),
      placeholderFontSize: 14,
      titleTextFontSize: 14); // RightListTile主题

  // 更新性别
  fetchUpdateGender(int gender) {
    ApiService(context).updateUserInfo(UserInfo(gender: gender)).then((value) {
      if (value.code == HttpConstants.SUCCEED_CODE) {
        UserModel model = Provider.of<UserModel>(context, listen: false);
        model.gender = gender;
        Navigator.pop(context);
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('编辑资料')),
      resizeToAvoidBottomInset: false,
      body: Consumer<UserModel>(builder: (context, model, child) {
        String gender = model.userInfo?.gender == 1 ? "男" : "女";

        return Padding(
          padding: const EdgeInsets.all(20),
          child: Column(
            children: [
              _buildAvatarItem(),
              const SizedBox(
                height: 58,
              ),
              CreatoRightListTile(
                title: const Text("姓名"),
                theme: theme,
                defaultTrailingText: "未设置",
                trailingText: model.userInfo?.name,
                onTap: () async {
                  String? name = await router.push<String>(
                    '/home/user_info/edit_info/name/${model.userInfo?.name}',
                  );
                  if (name == null || name.isEmpty) {
                    return;
                  }
                  model.name = name;
                },
              ),
              const CreatoDivider(),
              CreatoRightListTile(
                title: const Text("手机号"),
                theme: theme,
                defaultTrailingText: "未设置",
                trailingText: model.userInfo?.phoneNum,
                onTap: () {
                  Modal.bottomSheet(context,
                      child:
                          CreatoActionSheet(title: const Text("手机号"), actions: [
                        CreatoActionSheetAction(
                            child: const Text(
                              "换绑",
                            ),
                            onTap: () {
                              Navigator.pop(context);
                              router.push(
                                '/home/user_info/edit_account/phone',
                              );
                            })
                      ]));
                },
              ),
              const CreatoDivider(),
              CreatoRightListTile(
                title: const Text("邮箱"),
                theme: theme,
                defaultTrailingText: "未设置",
                trailingText: model.userInfo?.email,
                onTap: () {
                  router.push(
                    '/home/user_info/edit_account/email',
                  );
                },
              ),
              const CreatoDivider(),
              CreatoRightListTile(
                theme: theme,
                title: const Text("所在企业"),
                defaultTrailingText: "未设置",
                trailingText: model.userInfo?.enterprise,
                onTap: () async {
                  String? enterprise = await router.push<String>(
                    '/home/user_info/edit_info/enterprise/${model.userInfo?.enterprise}',
                  );
                  if (enterprise == null || enterprise.isEmpty) {
                    return;
                  }
                  model.enterprise = enterprise;
                },
              ),
              const CreatoDivider(),
              CreatoRightListTile(
                theme: theme,
                title: const Text("职务"),
                defaultTrailingText: "未设置",
                trailingText: model.userInfo?.position,
                onTap: () async {
                  String? position = await router.push<String>(
                    '/home/user_info/edit_info/position/${model.userInfo?.position}',
                  );
                  if (position == null || position.isEmpty) {
                    return;
                  }
                  model.position = position;
                },
              ),
              const CreatoDivider(),
              CreatoRightListTile(
                theme: theme,
                title: const Text("性别"),
                defaultTrailingText: "未设置",
                trailingText: model.userInfo?.gender == null ? null : gender,
                onTap: () {
                  Modal.bottomSheet(context,
                      child: CreatoActionSheet(
                        title: const Text("选择性别"),
                        actions: [
                          CreatoActionSheetAction(
                            selected: gender == "男",
                            onTap: () {
                              fetchUpdateGender(1);
                            },
                            child: const Text("男"),
                          ),
                          CreatoActionSheetAction(
                            selected: gender == "女",
                            onTap: () {
                              fetchUpdateGender(2);
                            },
                            child: const Text("女"),
                          )
                        ],
                      ));
                },
              ),
            ],
          ),
        );
      }),
    );
  }

  Widget _buildAvatarItem() {
    return Center(
      child: Column(
        children: [
          CircleAvatar(
            foregroundImage: Image.network(
                    "https://q5.itc.cn/q_70/images03/20240406/2c8f1d2cd13e4d4392fa07ccaab1f219.jpeg")
                .image,
            maxRadius: 80,
          ),
          const SizedBox(
            height: 14,
          ),
          const Text("点击更换")
        ],
      ),
    );
  }
}
